﻿using CSharpTestProject.Utils;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace CSharpTestProject.AdminFormFolder
{
    public partial class FindTeacherForm : Form
    {
        public _Sql sql;
        string[] s1, s2;
        public FindTeacherForm()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox1.SelectedIndex;
            if (index == -1) return;
            if (!s2[index].EndsWith(";"))
            {
                s2[index] += ";";
            }
            string[] temp = s2[index].Split(';');
            string t = temp[0];
            temp[0] = temp[temp.Length - 1];
            temp[temp.Length - 1] = t;
            comboBox2.DataSource = temp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "" || comboBox2.Text == "")
            {
                MessageBox.Show("请选择完整的信息！");
                return;
            }
            string s = $"SELECT xh,name,sj,sfqq,kcm,bz FROM dmb WHERE kcm='{comboBox2.Text}'";
            DataSet ds = sql.SqlFind(s);
            dataGridView1.ReadOnly = true;
            //不允许添加行
            dataGridView1.AllowUserToAddRows = false;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //平均分配列宽
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            // 展示在DataGridView上
            ds.Tables[0].Columns["xh"].ColumnName = "学号";
            ds.Tables[0].Columns["name"].ColumnName = "姓名";
            ds.Tables[0].Columns["sj"].ColumnName = "时间";
            ds.Tables[0].Columns["sfqq"].ColumnName = "是否出勤";
            ds.Tables[0].Columns["kcm"].ColumnName = "课程名";
            ds.Tables[0].Columns["bz"].ColumnName = "备注";
            dataGridView1.DataSource = ds.Tables[0];
            
        }

        private void FindTeacherForm_Load(object sender, EventArgs e)
        {
            string s = $"SELECT teachername,kcm FROM teachers";
            DataSet ds = sql.SqlFind(s);
            s1 = new string[ds.Tables[0].Rows.Count + 1];
            s2 = new string[ds.Tables[0].Rows.Count + 1];
            s1[0] = "";s2[0] = "";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                s1[i+1] = ds.Tables[0].Rows[i][0].ToString().Trim();
                s2[i+1] = ds.Tables[0].Rows[i][1].ToString().Trim();
            }
            
            comboBox1.DataSource = s1;
        }
    }
}
